<?php

namespace Biz;


/**
 * @see http://lbsyun.baidu.com/index.php?title=webapi
 */
class Baidu
{
    private $output = 'json';
    private $http;
    private $ak;
    private $sn;
    private $baseUrl = 'http://api.map.baidu.com';
    
    function __construct($config)
    {
        $this->ak = $config['ak'];
        $this->sn = $config['sn'];
        $this->http = new \GuzzleHttp\Client(['base_uri' => $this->baseUrl]);
    }

    public function geocoder($address)
    {
        $url = API.'/geocoder/v2';
    }


    private function sendRequest($resource, $params, $method = 'GET')
    {
        switch ($method) {
             case 'GET':

        }
    }

    /**
     * 根据经纬度获取地址
     *
     * @param string $lng 经
     * @param string $lat 纬
     * @return void
     */
    public function location($lng, $lat)
    {
        $query = [
            'ak' => $this->ak,
            'location' => "{$lat},{$lng}",
            'output' => $this->output,
            // 'sn' => $this->sn,
        ];
        $url = '/geocoder/v2/';
        $response = $this->http->get($url, [
            'query' => $query,
        ]);
        $json = json_decode((string)$response->getBody(), true);
        $rst = $json['result'];
        if ($rst['cityCode'] > 0)  {
            return $rst;
        } else {
            return false;
        }
    }


    /**
     * Undocumented function
     *
     * @param [type] $ip
     * @param string $coor 设置返回位置信息中，经纬度的坐标类型，分别如下：
                *coor不出现、或为空：百度墨卡托坐标，即百度米制坐标；
                *coor = bd09ll：百度经纬度坐标，在国测局坐标基础之上二次加密而来；
                *coor = gcj02：国测局02坐标，在原始GPS坐标基础上，按照国家测绘行业统一要求，加密后的坐标；
                *注：百度地图的坐标类型为bd09ll，如果结合百度地图使用，请注意坐标选择。
     * @return void
     */
    public function ipLocation($ip = null, $coor = '')
    {
        // $url = 'https://api.map.baidu.com/highacciploc/v1';
        //https://api.map.baidu.com/highacciploc/v1?qcip=220.181.38.113&ak=
        $url = '/location/ip';

        $query = [
            'ip' => $ip,
            'ak' => $this->ak,
            // 'sn' => $this->sn,
            'output' => $this->output,
            'coor' => $coor,
        ];
        $response = $this->http->get($url, [
            'query' => $query
        ]);
        $json = json_decode((string)$response->getBody(), true);
        if ($json['status'] === 0) {
            return $json;
        } else {
            return false;
        }
    }

    /**
     * 根据地址获取经纬度
     *
     * @return void
     */
    public function parse($address)
    {
        $url = '/geocoder/v2/';
        $query = [
            'address' => $address,
            'ak' => $this->ak,
            'output' => $this->output,
            // 'sn' => $this->sn,
        ];
        $response = $this->http->get($url, [
            'query' => $query
        ]);
        
        $json = json_decode((string)$response->getBody(), true);
        if ($json['status'] === 0) {
            return $json['result'];
        } else {
            return false;
        }
    }

    private function caculateAKSN($ak, $sk, $url, $querystring_arrays, $method = 'GET')
    {  
        if ($method === 'POST'){  
            ksort($querystring_arrays);  
        }  
        $querystring = http_build_query($querystring_arrays);  
        return md5(urlencode($url.'?'.$querystring.$sk));  
    }
}
